<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\logic;

use app\common\logic\BaseLogic;
use app\common\model\File;
use app\common\model\FileCate;
use think\db\exception\DbException;
use think\facade\Event;
use think\facade\Filesystem;

/**
 * 文件逻辑
 */
class FileLogic extends BaseLogic
{
    protected File $modelFile;
    protected FileCate $modelFileCate;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->modelFile = new File();
        $this->modelFileCate = new FileCate();
    }

    /**
     * 获取列表
     */
    public function list($params, bool $onlyTrashed = false)
    {
        return $this->modelFile->setParams($params)->getPage($onlyTrashed);
    }

    /**
     * 获取分类列表
     */
    public function listCate($params, bool $onlyTrashed = false)
    {
        return $this->modelFileCate->setParams($params)->getPage($onlyTrashed);
    }

    /**
     * 移动文件
     * @param $params
     * @return bool
     */
    public function move($params): bool
    {
        $result = $this->modelFile->setParams(['cid' => $params['cid']])->excludeFields(['password'])->edit($params['ids']);
        if (!$result) {
            $this->throw($this->modelFile->getError());
        }
        return $result;
    }

    /**
     * 重命名文件
     * @param $params
     * @return bool
     */
    public function rename($params): bool
    {
        $result = $this->modelFile->setParams(['name' => $params['name']])->excludeFields(['password'])->edit($params['id']);
        if (!$result) {
            $this->throw($this->modelFile->getError());
        }
        return $result;
    }

    /**
     * 批量删除文件
     * @param $id
     * @return bool
     */
    public function del($id): bool
    {
        $result = $this->modelFile->del($id);
        if (!$result) {
            $this->throw($this->modelFile->getError());
        }
        return $result;
    }

    /**
     * 添加文件分类
     * @param $params
     * @return bool
     */
    public function addCate($params): bool
    {
        $result = $this->modelFileCate->setParams([
            'type' => $params['type'],
            'parent_id' => $params['parent_id'],
            'name' => $params['name']
        ])->add();
        if (!$result) {
            $this->throw($this->modelFileCate->getError());
        }
        return $result;
    }

    /**
     * 编辑文件分类
     * @param $params
     * @return bool
     */
    public function editCate($params): bool
    {
        $result = $this->modelFileCate->setParams([
            'name' => $params['name'],
            'update_time' => time()
        ])->edit($params['id']);
        if (!$result) {
            $this->throw($this->modelFileCate->getError());
        }
        return $result;
    }

    /**
     * 删除文件分类
     * @param $id
     * @return bool
     */
    public function delCate($id): bool
    {
        $result = $this->modelFileCate->del($id);
        if (!$result) {
            $this->throw($this->modelFileCate->getError());
        }
        return $result;
    }

    /**
     * 获取文件
     * @param $params
     */
    public function getFile($params)
    {
        if (empty($params['suffix'])) {
            $params['suffix'] = false;
        }
        if (empty($params['uuid'])) {
            $this->throw('uuid参数错误');
        }
        $uuid = $params['uuid'];
        try {
            $model = $this->modelFile->where('uuid', $uuid)->find();
        } catch (DbException $e) {
            $this->throw($e->getMessage());
        }
        if (empty($model)) {
            $this->throw('文件不存在');
        }
        $background = '';
        if (!empty($params['background'])) {
            $background = $params['background'];
        }
        $suffix = (bool)$params['suffix'];
        $file = new \app\common\service\File($model->path, $suffix, $background);
        $file->name($model->name)->output();
    }
}